package cn.edu.scau.nursingcenter.common;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.edu.scau.nursingcenter.bean.ResultBean;


/**
 * 全局异常控制类
 * @author feng
 *
 */

@ControllerAdvice(basePackages = {"cn.edu.scau.nursingcenter","cn.edu.scau.nursingcenter.controller","cn.edu.scau.nursingcenter.dao","cn.edu.scau.nursingcenter.service.impl"})
public class GlobalExceptionController {

	
	/**
	  *  写法一：返回JSON数据，RuntimeException可以改为子类，实现更精确的异常捕获，就需要写更多的方法，包括自定义异常类
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(RuntimeException.class)
	@ResponseBody
	public ResultBean runtimeExceptionHandler(RuntimeException ex) {
		return new ResultBean(5005,false,ex.getMessage(),ex);
	}
	
//	/**
//	  *  写法二：返回指定页面，RuntimeException可以改为子类，实现更精确的异常捕获，就需要写更多的方法，包括自定义异常类
//	 * @param ex
//	 * @return
//	 */
//	@ExceptionHandler(RuntimeException.class)
//	public String runtimeExceptionHandler2(RuntimeException ex,Model model) {
//		model.addAttribute("error",ResultBean.builder().code(500).successed(false).message(ex.getMessage()).data(ex).build());
//		return "/error/exception";
//	}
	
	
//	@ExceptionHandler(MultipartException.class)
//    public String handleError1(MultipartException e, RedirectAttributes redirectAttributes)
//    {
//        redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
//        return "redirect:/uploadResult";
//    }

}
